package com.tanhua.server.service;

import com.alibaba.dubbo.config.annotation.Reference;
import com.github.tobato.fastdfs.domain.conn.FdfsWebServer;
import com.github.tobato.fastdfs.domain.fdfs.StorePath;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import com.tanhua.dubbo.server.api.SoundApi;
import com.tanhua.dubbo.server.pojo.Sound;
import com.tanhua.dubbo.server.pojo.SoundTimes;
import com.tanhua.server.pojo.User;
import com.tanhua.server.pojo.UserInfo;
import com.tanhua.server.utils.UserThreadLocal;
import com.tanhua.server.vo.SoundVo;
import org.apache.commons.lang3.StringUtils;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Collections;
import java.util.List;

@Service
public class SoundService {


    @Autowired
    private FastFileStorageClient fastFileStorageClient;

    @Autowired
    private FdfsWebServer fdfsWebServer;

    @Reference(version = "1.0.0")
    private SoundApi soundApi;

    @Autowired
    private UserInfoService userInfoService;

    public Boolean soundUpload(MultipartFile soundFile) {

        //token
        User user = UserThreadLocal.get();


        try {
            //上传语音
            StorePath storePath = fastFileStorageClient.uploadFile(soundFile.getInputStream(),
                    soundFile.getSize(),
                    StringUtils.substringAfter(soundFile.getOriginalFilename(), "."), null);

            //设置语音对象
            Sound sound = new Sound();
            sound.setUserId(user.getId());
            sound.setSoundUrl(fdfsWebServer.getWebServerUrl() + "/" + storePath.getFullPath());//设置音频文件路径
            soundApi.saveSound(sound);
            return true;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return false;
    }


    //搜索语音
    public SoundVo querySound() {

        SoundTimes soundTimes = null;
        try {
            User user = UserThreadLocal.get();
//            System.out.println(user);
            soundTimes = soundApi.queryTimes(user.getId());


//            System.out.println(soundTimes);

            if (soundTimes == null) {
                //设置查询次数
                soundTimes = new SoundTimes();
                soundTimes.setUserId(user.getId());
                soundTimes.setRemainingTimes(5);
                soundTimes.setDate(System.currentTimeMillis());
                Boolean bool = soundApi.saveTimes(soundTimes);
                if (!bool) {
                    return null;
                }
            }

            //上次登陆的时间
            Long oldDate = soundTimes.getDate();
            //现在的时间
            long now = System.currentTimeMillis();

            long time = now - oldDate;
            long l = 24 * 3600 * 1000;//24小时
            if (time > l) {
                soundApi.updateTimes(user.getId(), 5);
            }

            if (soundTimes.getRemainingTimes() == 0) {

                return null;
            }

            Integer times = soundTimes.getRemainingTimes();

            soundApi.updateTimes(user.getId(), times - 1);

            soundApi.updatedate(user.getId(), System.currentTimeMillis());


            UserInfo userInfo = userInfoService.queryUserInfoByUserId(user.getId());

            List<Sound> soundList = soundApi.querySoundList();

            Collections.shuffle(soundList);

            SoundVo soundVo = new SoundVo();
            ObjectId id = null;
            //填充数据
            for (Sound sound : soundList) {
                if (sound.getUserId().longValue() == userInfo.getUserId().longValue()) {
                    continue;
                }
                soundVo.setId(sound.getUserId().intValue());
                soundVo.setAvatar(userInfo.getLogo());
                soundVo.setNickname(userInfo.getNickName());
                soundVo.setGender(userInfo.getSex().name().toLowerCase());
                soundVo.setAge(userInfo.getAge());
                soundVo.setSoundUrl(sound.getSoundUrl());
                soundVo.setRemainingTimes(soundTimes.getRemainingTimes() - 1);
                id = sound.getId();
                break;
            }
            //删除该条
            soundApi.removeSound(id);


            return soundVo;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
